package com.zzl.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;

public class JsonUtil {
    public static JSONArray
    formatRsToJsonArray(ResultSet rs) throws SQLException {
        ResultSetMetaData md = rs.getMetaData();
        int num = md.getColumnCount();
        JSONArray array=new JSONArray();
        while (rs.next()){
            JSONObject mapOfColValues = new JSONObject();
            for(int i=1;i<=num;i++) {
                Object o = rs.getObject(i);
                if(o instanceof Date){
                    mapOfColValues.put(md.getColumnName(i),DateUtil.formatDate((Date)o,"yyyy-MM-dd"));
                }else{
                    mapOfColValues.put(md.getColumnName(i),rs.getObject(i));
                }
            }
            array.add(mapOfColValues);
        }
        return array;
    }
}
